package cn.aishua.ui.main;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import cn.aishua.R;
import cn.aishua.ui.MainActivityGroup;
import cn.aishua.ui.main.PopupMaker.OnFinishListener;
import cn.aishua.util.Constant;

public class FindActivity
    extends Activity
{

    TextView btnFindRateAll;
    TextView btnFindRate05;
    TextView btnFindRate610;
    TextView btnFindRate1120;
    TextView btnFindRate20Up;

    TextView textFindOk;

    //    View btnSpendRangeLeft;
    //    View btnSpendRangeRight;
    //    View btnSpendRangeMiddle;
    //    TextView textSpendRangeLeft;
    //    TextView textSpendRangeRight;
    Spinner spPrice;

    TextView textFindChooseArea;

    TextView textFindChooseCategory;

    LinearLayout linearPopupCanton;
    LinearLayout linearPopupCategory;

    // request parameters
    String SELECTED_RATE;
    int SELECTED_PARE_CATE = 0;
    int SELECTED_SUB_CATE = 0;
    int SELECTED_CANTON = 0;
    int SELECTED_BUSI_CIRC = 0;
    String LEFT_PRICE = "50";
    String RIGHT_PRICE = "-150";

    // page header widgets
    private ImageView ivLeftBtn;
    private ImageView ivRightBtn;
    private TextView tvTitile;
    
    MainActivityGroup parent ;

    private void initPageHeader()
    {
        // left button
        ivLeftBtn = (ImageView) findViewById( R.id.page_header_iv_left_btn );
        ivLeftBtn.setImageResource( R.drawable.btn_nav_header );
        ivLeftBtn.setOnClickListener( new View.OnClickListener()
        {

            @Override
            public void onClick( View v )
            {
                navigationBar( v );
            }
        } );

        // title
        tvTitile = (TextView) findViewById( R.id.page_header_tv_title );
        tvTitile.setText( "查找商户" );

        // right button
        ivRightBtn = (ImageView) findViewById( R.id.page_header_iv_right_btn );
        ivRightBtn.setImageResource( R.drawable.btn_shake );
        ivRightBtn.setOnClickListener( new View.OnClickListener()
        {

            @Override
            public void onClick( View v )
            {
                toShakePage( v );
            }
        } );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.find );
        initPageHeader();
        //new ResourceDataParser(MyKakaTestJsonData.RESOURCE).print();

        init();
        final PopupMaker pMaker1 = new PopupMaker( this, 0, 0 );
        pMaker1.make( linearPopupCanton, textFindChooseArea, 1, new OnFinishListener()
        {
            public void finish()
            {
                switchViewsForFind( true );
                SELECTED_CANTON = pMaker1.pId;
                SELECTED_BUSI_CIRC = pMaker1.id;
                parent.needsOwnBack = false;
            }
        } );

        final PopupMaker pMaker2 = new PopupMaker( this, 0, 0 );
        pMaker2.make( linearPopupCategory, textFindChooseCategory, 0, new OnFinishListener()
        {
            public void finish()
            {
                switchViewsForFind( true );
                SELECTED_PARE_CATE = pMaker2.pId;
                SELECTED_SUB_CATE = pMaker2.id;
                parent.needsOwnBack = false;
            }
        } );
    }

    private void init()
    {
        btnFindRateAll = (TextView) findViewById( R.id.btnFindRateAll );
        btnFindRate05 = (TextView) findViewById( R.id.btnFindRate05 );
        btnFindRate610 = (TextView) findViewById( R.id.btnFindRate610 );
        btnFindRate1120 = (TextView) findViewById( R.id.btnFindRate1120 );
        btnFindRate20Up = (TextView) findViewById( R.id.btnFindRate20Up );
        
        parent = (MainActivityGroup) getParent();

        spPrice = (Spinner) findViewById( R.id.find_sp_price );
        ArrayAdapter<NameValuePair> adapter1 = new ArrayAdapter<NameValuePair>( this, R.layout.find_spinner,
            getPriceList() );
        adapter1.setDropDownViewResource( R.layout.my_spinner_shake_dropdown );
        spPrice.setAdapter( adapter1 );

        btnFindRateAll.setOnClickListener( new OnClickListener()
        {
            public void onClick( View v )
            {
                clickRate( 0 );
            }
        } );
        btnFindRate05.setOnClickListener( new OnClickListener()
        {
            public void onClick( View v )
            {
                clickRate( 1 );
            }
        } );
        btnFindRate610.setOnClickListener( new OnClickListener()
        {
            public void onClick( View v )
            {
                clickRate( 2 );
            }
        } );
        btnFindRate1120.setOnClickListener( new OnClickListener()
        {
            public void onClick( View v )
            {
                clickRate( 3 );
            }
        } );
        btnFindRate20Up.setOnClickListener( new OnClickListener()
        {
            public void onClick( View v )
            {
                clickRate( 4 );
            }
        } );
        btnFindRate05.performClick();

        textFindOk = (TextView) findViewById( R.id.textFindOk );
        textFindOk.setOnClickListener( textFindOkClickListener );

        //        btnSpendRangeLeft = (View) findViewById( R.id.btnSpendRangeLeft );
        //        btnSpendRangeRight = (View) findViewById( R.id.btnSpendRangeRight );
        //        btnSpendRangeMiddle = (View) findViewById( R.id.btnSpendRangeMiddle );
        //        textSpendRangeLeft = (TextView) findViewById( R.id.textSpendRangeLeft );
        //        textSpendRangeRight = (TextView) findViewById( R.id.textSpendRangeRight );
        //        btnSpendRangeLeft.setOnTouchListener( btnSpendRangeLeftTouchListener );
        //        btnSpendRangeRight.setOnTouchListener( btnSpendRangeRightTouchListener );

        linearPopupCanton = (LinearLayout) findViewById( R.id.linearPopupCanton );
        linearPopupCanton.setVisibility( View.GONE );

        linearPopupCategory = (LinearLayout) findViewById( R.id.linearPopupCategory );
        linearPopupCategory.setVisibility( View.GONE );

        textFindChooseArea = (TextView) findViewById( R.id.textFindChooseArea );

        textFindChooseArea.setOnClickListener( new OnClickListener()
        {
            public void onClick( View v )
            {
                linearPopupCanton.setVisibility( View.VISIBLE );
                parent.needsOwnBack = true;
                switchViewsForFind( false );
            }
        } );

        textFindChooseCategory = (TextView) findViewById( R.id.textFindChooseClass );

        textFindChooseCategory.setOnClickListener( new OnClickListener()
        {
            public void onClick( View v )
            {
                linearPopupCategory.setVisibility( View.VISIBLE );
                parent.needsOwnBack = true;
                switchViewsForFind( false );
            }
        } );

    }

    private List<NameValuePair> getPriceList()
    {
        List<NameValuePair> priceList = new ArrayList<NameValuePair>();
        priceList.add( new NameValuePair( "全部", "" ) );
        priceList.add( new NameValuePair( "0-50元", "0-50" ) );
        priceList.add( new NameValuePair( "50-100元", "50-100" ) );
        priceList.add( new NameValuePair( "100-200元", "100-200" ) );
        priceList.add( new NameValuePair( "200-500元", "200-500" ) );
        priceList.add( new NameValuePair( "500以上", "500" ) );
        return priceList;
    }

    //    merchant_name:xxx, parent_categories[]:xxx, sub_categories[]:xxx, cantons[]:xxx ,
    //    *            business_circles[]:xxx, average_price[]:xxx-xxx, max_customer_rate[]:xx-xx,
    //    *            select_type: x, page:x, sort:x

    OnClickListener textFindOkClickListener = new OnClickListener()
    {
        public void onClick( View v )
        {
            Intent intent = new Intent( FindActivity.this, VendorListActivity.class );
            intent.putExtra( Constant.EXTRA_BOOL_IS_SEARCH, true );

            /**
             * When user selects distance as canton criteria, other criteria will be ignored. Search
             * for nearby vendors will be used. When SELECTED_BUSI_CIRC == 0, it means canton and
             * business circle should be ignored, other criteria is still valid
             */
            intent.putExtra( Constant.EXTRA_INT_CRITERIA_PARE_CATE, SELECTED_PARE_CATE );
            intent.putExtra( Constant.EXTRA_INT_CRITERIA_SUB_CATE, SELECTED_SUB_CATE );

            intent.putExtra( Constant.EXTRA_INT_CRITERIA_CANTON, SELECTED_CANTON );
            intent.putExtra( Constant.EXTRA_INT_CRITERIA_BUSI_CIRC, SELECTED_BUSI_CIRC );

            intent.putExtra( Constant.EXTRA_STR_CRITERIA_AVER_PRICE,
                ( (NameValuePair) spPrice.getSelectedItem() ).getValue() );
            intent.putExtra( Constant.EXTRA_STR_CRITERIA_RATE, SELECTED_RATE );

            Log.d( "FindActivity", intent.getExtras().toString() );
            startActivity( intent );
        }
    };

    private void clickRate( int which )
    {
        btnFindRateAll.setBackgroundColor( Color.parseColor( "#f0efef" ) );
        btnFindRate05.setBackgroundColor( Color.parseColor( "#f0efef" ) );
        btnFindRate610.setBackgroundColor( Color.parseColor( "#f0efef" ) );
        btnFindRate1120.setBackgroundColor( Color.parseColor( "#f0efef" ) );
        btnFindRate20Up.setBackgroundColor( Color.parseColor( "#f0efef" ) );

        switch( which )
        {
            case 0:
                btnFindRateAll.setBackgroundResource( R.drawable.find_rate_selected_back );
                SELECTED_RATE = "";
                break;
            case 1:
                btnFindRate05.setBackgroundResource( R.drawable.find_rate_selected_back );
                SELECTED_RATE = "0-5";
                break;
            case 2:
                btnFindRate610.setBackgroundResource( R.drawable.find_rate_selected_back );
                SELECTED_RATE = "6-10";
                break;
            case 3:
                btnFindRate1120.setBackgroundResource( R.drawable.find_rate_selected_back );
                SELECTED_RATE = "11-20";
                break;
            case 4:
                btnFindRate20Up.setBackgroundResource( R.drawable.find_rate_selected_back );
                SELECTED_RATE = "20";
                break;
        }

    }

    //    //------------------Avg spending begin
    //    int initX = 0;
    //    int moveX = 0;
    //    OnTouchListener btnSpendRangeLeftTouchListener = new OnTouchListener()
    //    {
    //        public boolean onTouch( View v, MotionEvent event )
    //        {
    //            if( event.getAction() == MotionEvent.ACTION_DOWN )
    //            {
    //                initX = (int) event.getRawX();
    //                return true;
    //            }
    //            if( event.getAction() == MotionEvent.ACTION_MOVE )
    //            {
    //                moveX = (int) event.getRawX() - initX;
    //                initX = (int) event.getRawX();
    //                int x = ( (AbsoluteLayout.LayoutParams) btnSpendRangeLeft.getLayoutParams() ).x + moveX;
    //
    //                if( x <= 0 )
    //                    x = 0;
    //                int maxX = -50 + ( (AbsoluteLayout.LayoutParams) btnSpendRangeRight.getLayoutParams() ).x;
    //                if( x >= maxX )
    //                    x = maxX;
    //
    //                AbsoluteLayout.LayoutParams btnParams = (AbsoluteLayout.LayoutParams) btnSpendRangeLeft
    //                    .getLayoutParams();
    //                btnSpendRangeLeft.setLayoutParams( new AbsoluteLayout.LayoutParams( btnParams.width, btnParams.height,
    //                    x, btnParams.y ) );
    //                computerBtnSpendRangeMiddle();
    //
    //                AbsoluteLayout.LayoutParams textParams = (AbsoluteLayout.LayoutParams) textSpendRangeLeft
    //                    .getLayoutParams();
    //                textSpendRangeLeft.setLayoutParams( new AbsoluteLayout.LayoutParams( textParams.width,
    //                    textParams.height, x + 5, textParams.y ) );
    //                textSpendRangeLeft.setText( "￥" + x );
    //
    //                LEFT_PRICE = String.valueOf( x );
    //                return true;
    //            }
    //
    //            return true;
    //        }
    //    };
    //
    //    private void computerBtnSpendRangeMiddle()
    //    {
    //        int x = ( (AbsoluteLayout.LayoutParams) btnSpendRangeLeft.getLayoutParams() ).x + 50;
    //        int width = ( (AbsoluteLayout.LayoutParams) btnSpendRangeRight.getLayoutParams() ).x - x;
    //        btnSpendRangeMiddle.setLayoutParams( new AbsoluteLayout.LayoutParams( width, -1, x, 0 ) );
    //    }

    //    OnTouchListener btnSpendRangeRightTouchListener = new OnTouchListener()
    //    {
    //        public boolean onTouch( View v, MotionEvent event )
    //        {
    //            if( event.getAction() == MotionEvent.ACTION_DOWN )
    //            {
    //                initX = (int) event.getRawX();
    //                return true;
    //            }
    //            if( event.getAction() == MotionEvent.ACTION_MOVE )
    //            {
    //                moveX = (int) event.getRawX() - initX;
    //                initX = (int) event.getRawX();
    //                int x = ( (AbsoluteLayout.LayoutParams) btnSpendRangeRight.getLayoutParams() ).x + moveX;
    //
    //                if( x >= 320 )
    //                    x = 320;
    //                int minX = 50 + ( (AbsoluteLayout.LayoutParams) btnSpendRangeLeft.getLayoutParams() ).x;
    //                if( x <= minX )
    //                    x = minX;
    //
    //                AbsoluteLayout.LayoutParams btnParams = (AbsoluteLayout.LayoutParams) btnSpendRangeLeft
    //                    .getLayoutParams();
    //
    //                btnSpendRangeRight.setLayoutParams( new AbsoluteLayout.LayoutParams( btnParams.width, btnParams.height,
    //                    x, btnParams.y ) );
    //                computerBtnSpendRangeMiddle();
    //
    //                AbsoluteLayout.LayoutParams textParams = (AbsoluteLayout.LayoutParams) textSpendRangeRight
    //                    .getLayoutParams();
    //
    //                textSpendRangeRight.setLayoutParams( new AbsoluteLayout.LayoutParams( textParams.width,
    //                    textParams.height, x + 35, textParams.y ) );
    //                int upPrice = ( x + 50 );
    //                String text = "";
    //                if( upPrice < 300 )
    //                {
    //                    text = "￥" + upPrice;
    //                    RIGHT_PRICE = "-" + upPrice;
    //                }
    //                else if( upPrice >= 300 && upPrice <= 350 )
    //                {
    //                    text = "￥" + 300;
    //                    RIGHT_PRICE = "-300";
    //                }
    //                else
    //                {
    //                    text = "不限";
    //                    RIGHT_PRICE = "";
    //                }
    //                textSpendRangeRight.setText( text );
    //                return true;
    //            }
    //
    //            return true;
    //        }
    //    };

    //------------------Avg spending end

    //------------------ area choose block begin

    private void switchViewsForFind( boolean what )
    {
        textFindChooseArea.setEnabled( what );
        textFindChooseCategory.setEnabled( what );

        btnFindRateAll.setEnabled( what );
        btnFindRate05.setEnabled( what );
        btnFindRate610.setEnabled( what );
        btnFindRate1120.setEnabled( what );
        btnFindRate20Up.setEnabled( what );

        textFindOk.setEnabled( what );

        //        btnSpendRangeLeft.setEnabled( what );
        //        btnSpendRangeRight.setEnabled( what );

    }

    //------------------ area choose block end

    //------------------ class choose block begin

    //    private void fixInvalidateBug()
    //    {
    //        LinearLayout linearFindAvgSpendingSlide = (LinearLayout) findViewById( R.id.linearFindAvgSpendingSlide );
    //        linearFindAvgSpendingSlide.invalidate();
    //
    //        LinearLayout linearFindAvgSpending = (LinearLayout) findViewById( R.id.linearFindAvgSpending );
    //        linearFindAvgSpending.invalidate();
    //
    //        LinearLayout btnFindRateGroup = (LinearLayout) findViewById( R.id.btnFindRateGroup );
    //        btnFindRateGroup.invalidate();
    //
    //        LinearLayout linearFindRateGroup = (LinearLayout) findViewById( R.id.linearFindRateGroup );
    //        linearFindRateGroup.invalidate();
    //    }

    //------------------ class choose block end

    public void navigationBar( View view )
    {
        MainActivityGroup parent = (MainActivityGroup) getParent();
        parent.handleNaviBar();
    }

    public void toShakePage( View view )
    {
        ( (MainActivityGroup) getParent() ).ignoreNaviBar = true;
        ( (MainActivityGroup) getParent() ).switchActivityForFixTabs( 3 );
        //        ( (MainActivity) getParent() ).ignoreNaviBar = false;
        //        Intent intent = new Intent( this, MainActivity.class );
        //        intent.putExtra( MainActivity.TAB, MainActivity.TAG_SHAKE );
        //        startActivity( intent );
    }

    @Override
    public void onBackPressed()
    {
        View whoPopped = null;
        if( linearPopupCanton.getVisibility() == View.VISIBLE )
        {
            whoPopped = linearPopupCanton;
        }

        if( linearPopupCategory.getVisibility() == View.VISIBLE )
        {
            whoPopped = linearPopupCategory;
        }

        if( whoPopped != null )
        {
            whoPopped.setVisibility( View.GONE );
            switchViewsForFind( true );
            parent.needsOwnBack = false;
            //            fixInvalidateBug();
            return;
        }

        super.onBackPressed();
    }

    class NameValuePair
    {
        private String name; // displayed in spinner
        private String value;

        public NameValuePair(String name, String value)
        {
            super();
            this.name = name;
            this.value = value;
        }

        public String getName()
        {
            return name;
        }

        public void setName( String name )
        {
            this.name = name;
        }

        public String getValue()
        {
            return value;
        }

        public void setValue( String value )
        {
            this.value = value;
        }

        @Override
        public String toString()
        {
            return name;
        }
    }

}
